package com.springCloud.service.impl;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.springCloud.domain.Message;
import com.springCloud.service.ConsumerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 消费者服务
 *
 * @author zhu
 * @version 1.0
 * @date 2021/5/27 17:31
 */
@Slf4j
@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Resource
    private EurekaClient eurekaClient;

    /**
     * 通过EurekaClient请求服务
     *
     * @return 请求结果
     */
    @Override
    public Message queryByClient() {
        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("Provider", false);
        String url = instanceInfo.getHomePageUrl();
        log.info("请求连接：{}", url);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Message> responseEntity =  restTemplate.getForEntity(url, Message.class);
        return responseEntity.getBody();
    }

    /**
     * 通过Feign请求服务
     *
     * @return 请求结果
     */
    @Override
    public Message queryByFeign() {
        return null;
    }
}
